/*
 *     This file is part of Neptune Network Emulator.
 *
 *  Neptune Network Emulator is free software: 
 *  you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Neptune Network Emulator is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Neptune Network Emulator.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/**
 *
 */
package neptune.web.client.view;


import neptune.web.client.WebInterface;
import neptune.web.client.services.experiment.TemplateService;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * @author Luigi Lorenzo Arpino [luigiarpino@yahoo.it]
 */
public class TemplatesSummaryView extends View
{
	public static final String DEFAULT_TOKEN = "TemplatesSummaryView";
	private FlexTable templatesTable;
	private String[][] templatesList;
	
	
	public TemplatesSummaryView()
	{
		// Setting view params
		try 
		{
			this.setName(TemplatesSummaryView.DEFAULT_TOKEN);
		}
		catch (DuplicatedTokenException e) 
		{
			ConsoleManager.addConsoleDebugMessage(e.getMessage(), true);
		}

		// Costruzione struttura interfaccia
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setSize("90%", "100%");

		// Header tabella PM
		FlexTable pmTableHeader = new FlexTable();
		pmTableHeader.setWidth("98%");
		pmTableHeader.setText(0, 0, "");
		pmTableHeader.setText(0, 1, "Name");
		pmTableHeader.setText(0, 2, "Description");
		
		
		//pmTableHeader.getColumnFormatter().setWidth(0, "16px");
		pmTableHeader.getColumnFormatter().setWidth(0, "10%");
		pmTableHeader.getColumnFormatter().setWidth(1, "25%");
		pmTableHeader.getColumnFormatter().setWidth(2, "65%");
		
		vPanel.add(pmTableHeader);

		templatesTable = new FlexTable();
		templatesTable.setWidth("98%");
		ScrollPanel pmTableWrap = new ScrollPanel(templatesTable);
		pmTableWrap.setSize("100%", "400px");
		vPanel.add(pmTableWrap);

		
		HorizontalPanel horizontalPanel=new HorizontalPanel();
		//horizontalPanel.add(new Label("OLA"));
	
		VerticalPanel mainPanel=new VerticalPanel();
		mainPanel.add(horizontalPanel);
		mainPanel.add(vPanel);
		
		initWidget(mainPanel);

		// Styles
		vPanel.setStylePrimaryName("ClusterView");
		pmTableHeader.setStylePrimaryName("ClusterView-pmTable");
		pmTableHeader.addStyleDependentName("header");
		pmTableWrap.setStylePrimaryName("ClusterView-pmTableWrap");
		templatesTable.setStylePrimaryName("ClusterView-pmTable");
	
	}
	
	public void readDataFromServer()
	{
		TemplateService.Util.getInstance().getAllTemplate(new AsyncCallback<String[][]>()
		{

			public void onFailure(Throwable caught) 
			{
				String message=new String("Error Unable to read templates list: "+caught.getMessage() );
            	WebInterface.addMessageToConsoleAndShowDialog(message,true);
			}

			public void onSuccess(String[][] result) 
			{
				templatesList=result;
				handlerData();
			}
		});
	}

	/**
	 * @param result
	 */
	private void handlerData()
	{
		// cancello le righe
		int row = templatesTable.getRowCount();
		for (int i=0 ; i<row ;i++)
		{
			templatesTable.removeRow(0);
		}
		for (int i=0 ; i<templatesList.length ;i++)
		{
			System.out.println(templatesList[i][0]);
			templatesTable.setWidget(i, 0, new TemplatesPanel(templatesList[i][0], templatesList[i][1],templatesList[i][2],i) );
		}	
	}
	
	private class TemplatesPanel extends Composite implements ClickListener
	{
	
		public TemplatesPanel(String fileName, String name, String description, int row)
		{
			VerticalPanel vPanel = new VerticalPanel();
			vPanel.setSize("100%", "100%");
	
			Button btDelete=new Button("Delete");
			btDelete.setTitle("Delete template <<"+name+">> with fileName=\""+fileName+"\"");
			btDelete.addClickListener(this);
			FlexTable dataTable = new FlexTable();
			dataTable.setWidth("100%");
			dataTable.setWidget(0, 0, btDelete);
			
			//DateTimeFormat format=new DateTimeFormat("");
			//("yyyy-MM-dd HH:mm:ss");
			//= DateTimeFormat("yyyy-MM-dd HH:mm:ss");
			//Date date = format.parse(String.valueOf( System.currentTimeMillis() ) );
			//Date date=new Date(System.currentTimeMillis());
			dataTable.setText(0, 1, name);
			dataTable.setText(0, 2, description);
			
			//dataTable.getColumnFormatter().setWidth(0, "16px");
			dataTable.getColumnFormatter().setWidth(0, "10%");
			dataTable.getColumnFormatter().setWidth(1, "25%");
			dataTable.getColumnFormatter().setWidth(2, "65%");
						
			vPanel.add(dataTable);
	
			initWidget(vPanel);
			// Styles
			vPanel.setStylePrimaryName("ClusterView-NodePanel");
			dataTable.setStylePrimaryName("ClusterView-NodePanel-dataTable");
			/*
			if (row%2 == 1)
			{
				dataTable.setStylePrimaryName("ClusterView-NodePanel-dataTable");
			}
			*/
			
		}

		/* (non-Javadoc)
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget sender)
		{
			// Leggo il nome del file da cancellare
            String title = ((ButtonBase) sender).getTitle();
            String tagInfo = title.split("\"")[1];
			boolean isConfirmed = Window.confirm(title+" ?");
			if (isConfirmed) 
			{
				TemplateService.Util.getInstance().deleteTemplate(tagInfo, new AsyncCallback<Boolean>()
				{

					public void onFailure(Throwable caught) 
					{
						String message = new String("\nError: Unable to delete topology template file\n"
                                                + caught.getMessage());
						WebInterface.addMessageToConsoleAndShowDialog(message, true);
					}

					public void onSuccess(Boolean result) 
					{
						readDataFromServer();
						String message = new String("The topology template file has been removed!");
						WebInterface.addMessageToConsoleAndShowDialog(message, false);
					}
				});
			}
		}
	}
}
